package com.xiaomaoguai.crs.common.utils;

import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;

/**
 * @ FileName: WebUtils.java
 * @ Author: WeiHui-Z
 * @ Date: 2016-12-21  10:35
 * @ Version: v1.0.0
 */
public final class WebUtils {

	private WebUtils() {
	}

	/**
	 * 判断当前请求是否是ajax请求
	 *
	 * @param request 请求
	 * @return true or false
	 */
	public static boolean isAjaxRequest(HttpServletRequest request) {
		Enumeration<String> names = request.getHeaderNames();
		while (names.hasMoreElements()) {
			String name = names.nextElement();
			if (StringUtils.equalsIgnoreCase("X-Requested-With", name)) {
				String value = request.getHeader(name);
				return StringUtils.equalsIgnoreCase("XMLHttpRequest", value);
			}
		}
		return false;
	}
}
